/**
 * 消息相关接口
 */

import { get, post, patch, del } from '@/utils/request'

// API 响应格式
interface ApiResponse<T> {
  success: boolean
  statusCode: number
  message: string
  data: T
}

// 消息类型枚举
export enum MessageType {
  TEXT = 'text',
  IMAGE = 'image',
  VIDEO = 'video',
  AUDIO = 'audio',
  FILE = 'file'
}

// 用户信息
export interface MessageUser {
  id: number
  username?: string
  nickname?: string
  avatar?: string
}

// 消息信息
export interface MessageInfo {
  id: number
  conversationId: string
  senderId: number
  receiverId: number
  type: MessageType
  content: string
  mediaUrl?: string
  isRead: boolean
  readAt?: string
  createdAt: string
  sender?: MessageUser
  receiver?: MessageUser
}

// 会话信息
export interface ConversationInfo {
  conversationId: string
  otherUser: MessageUser
  lastMessageContent?: string
  lastMessageAt?: string
  unreadCount: number
  updatedAt: string
}

// 会话列表响应
export interface ConversationsResponse {
  data: ConversationInfo[]
  total: number
  page: number
  limit: number
  totalPages: number
}

// 消息列表响应
export interface MessagesResponse {
  data: MessageInfo[]
  total: number
  page: number
  limit: number
  totalPages: number
}

// 发送消息参数
export interface SendMessageParams {
  receiverId: number
  type?: MessageType
  content: string
  mediaUrl?: string
}

/**
 * 获取会话列表
 */
export const getConversations = async (
  page: number = 1,
  limit: number = 20
): Promise<ConversationsResponse> => {
  const queryParams: any = {
    page: String(page),
    limit: String(limit)
  }

  const response = await get<ApiResponse<ConversationsResponse>>(
    '/messages/conversations',
    queryParams
  )

  return response.data
}

/**
 * 获取会话消息列表
 */
export const getConversationMessages = async (
  conversationId: string,
  page: number = 1,
  limit: number = 50
): Promise<MessagesResponse> => {
  const queryParams: any = {
    page: String(page),
    limit: String(limit)
  }

  const response = await get<ApiResponse<MessagesResponse>>(
    `/messages/conversations/${conversationId}`,
    queryParams
  )

  return response.data
}

/**
 * 发送消息
 */
export const sendMessage = async (
  params: SendMessageParams
): Promise<MessageInfo> => {
  const response = await post<ApiResponse<MessageInfo>>(
    '/messages/send',
    params
  )

  return response.data
}

/**
 * 标记消息为已读
 */
export const markMessageAsRead = async (messageId: number): Promise<void> => {
  await post(`/messages/${messageId}/read`, {})
}

/**
 * 标记会话所有消息为已读
 */
export const markConversationAsRead = async (conversationId: string): Promise<void> => {
  await post(`/messages/conversations/${conversationId}/read`, {})
}

/**
 * 删除消息
 */
export const deleteMessage = async (messageId: number): Promise<void> => {
  await del(`/messages/${messageId}`)
}

// ==================== 通知相关 ====================

// 通知类型枚举
export enum NotificationType {
  FRIEND_REQUEST = 'friend_request',
  FRIEND_ACCEPTED = 'friend_accepted',
  WORK_LIKED = 'work_liked',
  WORK_COLLECTED = 'work_collected',
  WORK_COMMENTED = 'work_commented',
  COMMENT_REPLIED = 'comment_replied',
  SYSTEM = 'system'
}

// 通知信息
export interface NotificationInfo {
  id: number
  userId: number
  triggerId?: number | null
  type: NotificationType
  title: string
  content: string
  data?: Record<string, any> | null
  isRead: boolean
  readAt?: string | null
  createdAt: string
  trigger?: MessageUser
}

// 通知列表响应
export interface NotificationsResponse {
  data: NotificationInfo[]
  total: number
  page: number
  limit: number
  totalPages: number
}

/**
 * 获取通知列表
 */
export const getNotifications = async (
  page: number = 1,
  limit: number = 20,
  type?: NotificationType
): Promise<NotificationsResponse> => {
  const queryParams: any = {
    page: String(page),
    limit: String(limit)
  }
  if (type) {
    queryParams.type = type
  }

  const response = await get<ApiResponse<NotificationsResponse>>(
    '/messages/notifications',
    queryParams
  )

  return response.data
}

/**
 * 标记通知为已读
 */
export const markNotificationAsRead = async (notificationId: number): Promise<void> => {
  await post(`/messages/notifications/${notificationId}/read`, {})
}

/**
 * 标记所有通知为已读
 */
export const markAllNotificationsAsRead = async (): Promise<void> => {
  await post('/messages/notifications/read-all', {})
}

/**
 * 获取未读通知总数
 */
export const getUnreadNotificationsCount = async (): Promise<number> => {
  const response = await get<ApiResponse<number>>('/messages/notifications/unread/count')
  return response.data
}

